/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.server;

import nfse.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.*;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public abstract class NetFSEServiceServlet extends RemoteServiceServlet {

    protected Connection statsConnection = null;

    protected Connection metadataConnection = null;

    protected Connection resultConnection = null;
    
    public NetFSEServiceServlet() {
        super();
        try {

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (NetFSE.TYPE_CLASS_NAMES == null) {
            try {
                NetFSE.configure();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public String getUserName() {
		String userName = this.getThreadLocalRequest().getRemoteUser();
		if (userName == null || userName.length() == 0)
			userName = "";
		return userName;
	}

    public Statement createMetadataStatement() throws Exception {
        Statement s = null;
        try {
            s = metadataConnection.createStatement();
        } catch (Exception e) {
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://" + NetFSE.METADATA_HOST + "/" + NetFSE.METADATA_DATABASE;
            Properties props = new Properties();
            props.setProperty("user", NetFSE.METADATA_USER);
            props.setProperty("password", NetFSE.METADATA_PASSWORD);
            props.setProperty("client_encoding", "UTF8");

            metadataConnection = DriverManager.getConnection(url, props);

            s = metadataConnection.createStatement();
        }
        return s;
    }

    public Statement createResultStatement() throws Exception {
        Statement s = null;
        try {
            s = resultConnection.createStatement();
        } catch (Exception e) {
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://" + NetFSE.RESULT_DB_HOST + "/" + NetFSE.RESULT_DB_DATABASE;
            Properties props = new Properties();
            props.setProperty("user", NetFSE.RESULT_DB_USER);
            props.setProperty("password", NetFSE.RESULT_DB_PASSWORD);
            props.setProperty("client_encoding", "UTF8");

            try {
                resultConnection = DriverManager.getConnection(url, props);
            } catch (Exception e2) {
                e2.printStackTrace();
                url = "jdbc:postgresql://" + NetFSE.RESULT_DB_HOST + "/" + NetFSE.METADATA_DATABASE;
                Connection connectionMetadata = DriverManager.getConnection(url, props);
                Statement metadataStatement = connectionMetadata.createStatement();
                metadataStatement.execute("create database " + NetFSE.RESULT_DB_DATABASE);
                metadataStatement.close();
                connectionMetadata.close();
                resultConnection = DriverManager.getConnection(url, props);
            }

            s = resultConnection.createStatement();
        }
        return s;
    }

    public Statement createStatsStatement() {
        try {

            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://" + NetFSE.STATS_DB_HOST + "/" + NetFSE.STATS_DB_DATABASE;
            Properties props = new Properties();
            props.setProperty("user", NetFSE.STATS_DB_USER);
            props.setProperty("password", NetFSE.STATS_DB_PASSWORD);
            props.setProperty("client_encoding", "UTF8");

            if (statsConnection == null) {
                try {
                    statsConnection = DriverManager.getConnection(url, props);
                } catch (Exception e2) {
                    e2.printStackTrace();
                    url = "jdbc:postgresql://" + NetFSE.RESULT_DB_HOST + "/" + NetFSE.METADATA_DATABASE;
                    Connection connectionMetadata = DriverManager.getConnection(url, props);
                    Statement metadataStatement = connectionMetadata.createStatement();
                    metadataStatement.execute("create database " + NetFSE.STATS_DB_DATABASE);
                    metadataStatement.close();
                    connectionMetadata.close();
                    statsConnection = DriverManager.getConnection(url, props);
                }
            }
            Statement s = null;
            try {
                s = statsConnection.createStatement();
            } catch (Exception e2) {
                try {
                    statsConnection = DriverManager.getConnection(url, props);
                    s = statsConnection.createStatement();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    
}
